
app.controller('newFnCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope) {
	var token = $window.localStorage.getItem("token");
	$scope.iNowData = {
		"id":"",
		"methodName":"",
		"methodTagName":"",
		"menuLookup.id":"",
		"isSelected":"",
		"isCreated":"",
		"remark":""
	}
    $scope.init = function(){
        //初始化所属菜单请求
        rest.decoJava({
        	type: "post",
            url: '/tenancy-sys/admin/menu/all/list',
            data: "",
            beforeSend: function(request){
				request.setRequestHeader("token", token); 
			},
            success: function (result) {
                $scope.upMenuDatas = [{menuName: "请选择", id: ""}];
                angular.forEach(result.result,function(item){
                	if(item.level==1){
                		$scope.upMenuDatas.push(item);
                	}
                })
                //$scope.upMenuDatas = result.result;
                //$scope.upMenuDatas.unshift({menuName: "请选择", id: ""});
                $scope.menuLookup = $scope.upMenuDatas[0];
                $scope.$apply();
            }
        })
    };
    $scope.init();
    
    //保存
    $scope.preserveSubmit = function(){
        if ($scope.menuLookup == undefined) {
            var menuLookupID = "";
        }else{
            var menuLookupID = $scope.menuLookup.id;
        }
        var formData = {
            "methodName": $scope.iNowData.methodName,
            "menuId" : menuLookupID,
            "isSelected" : $("#isSelected").val(),
            "remark" : $scope.iNowData.remark,
            "methodTagName": $scope.iNowData.methodTagName
        }
        
        //非空校验
        var btn = true;
        if($scope.iNowData.methodName == "" || $scope.iNowData.methodName == undefined){
        	layer.tips('方法名称必填', '.methodName-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        };
        if($scope.iNowData.methodTagName == ""){
        	layer.tips('关键字必填', '.methodTagName-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        };
        if(menuLookupID == ""){
        	layer.tips('所属菜单必填', '.menuLookup-Tips',{
			  tipsMore: true,
			  tips: [2,'#ed5565']
			});
    		btn =  false;
        };
        
        if(btn){
        	//保存
	    	rest.decoJava({
	    		type: 'post',
	            url: '/tenancy-sys/admin/menuMethod/add',
	            data: JSON.stringify(formData),
	            contentType: "application/json; charset=utf-8",
	            beforeSend: function(request){
					request.setRequestHeader("token", token); 
				},
	            success: function (result) {
	            	layer.closeAll('loading');
	            	console.log(result)
	                if (result) {
	                    if ("0000" == result.code) {
							layer.msg(result.message, {
					        	offset:'t',
					        	icon: 1,
								time: 0, //不自动关闭
								btn: ['确定'],
								yes: function(index){
									layer.close(index);
									$scope.closeThisDialog();
							    }
							});
	                    } else {
	                    	layer.msg(result.message,{
			    				offset:'t'
			    			});
	                    }
	                }
	            }
	        })
        };
    };
    
    $scope.hideBtn = function(){
    	$scope.closeThisDialog();
    }
    
});
